package com.clss.commandhelper.function.supportTeam;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.clss.commandhelper.R;
import com.clss.commandhelper.base.BaseActivity;
import com.clss.commandhelper.tools.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class SupportTeamListActivity extends BaseActivity implements SupportTeamListPresenter.SupportView,
        AdapterView.OnItemClickListener {

    @BindView(R.id.include_top_bar_left_iv)
    ImageView mIncludeTopBarLeftIv;
    @BindView(R.id.include_top_bar_title_tv)
    TextView mIncludeTopBarTitleTv;
    @BindView(R.id.include_top_bar_right_tv)
    TextView mIncludeTopBarRightTv;
    @BindView(R.id.police_lv)
    ListView mPoliceLv;

    private String taskId;
    private SupportTeamListPresenter mPresenter;
    private PoliceAdapter mPoliceAdapter;
    private List<PoliceInfoBean> datas;
    public Map<Integer,Boolean> map;
    private List<String> mIds;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int setViewId() {
        return R.layout.activity_support_team_list;
    }

    @Override
    protected void initView() {
        Bundle bundle = getIntent().getExtras();
        assert bundle != null;
        taskId = bundle.getString("id");
        datas = new ArrayList<>();
        map = new HashMap<>(16);
        mIds = new ArrayList<>();
        mPresenter = new SupportTeamListPresenter();
        mPresenter.setSupportView(this);
        mIncludeTopBarTitleTv.setText("请求支援");
        mIncludeTopBarRightTv.setText("提交");
        mIncludeTopBarRightTv.setVisibility(View.VISIBLE);
        mPoliceAdapter = new PoliceAdapter(this,0);
        mPoliceLv.setAdapter(mPoliceAdapter);
        mPoliceLv.setOnItemClickListener(this);
    }

    @Override
    protected void initData(@Nullable Bundle savedInstanceState) {
        mPresenter.getPoliceList();
    }

    @Override
    public void responseError(int code, String msg) {
        showShortToast(msg);
    }

    @OnClick({R.id.include_top_bar_left_iv, R.id.include_top_bar_right_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.include_top_bar_left_iv:
                finish();
                break;
            case R.id.include_top_bar_right_tv:
                mPresenter.askSupport();
                break;
            default:
        }
    }

    @Override
    public String getId(){
        return taskId;
    }

    @Override
    public List<String> getPoliceIds() {
        return mIds;
    }

    @Override
    public void setListData(List<PoliceInfoBean> beans, String error) {
        if (Utils.isNull(beans) || beans.size() == 0){
            showShortToast(error);
            return;
        }
        datas.clear();
        map.clear();
        for (int i = 0; i < beans.size(); i++) {
            map.put(i,false);
        }
        datas.addAll(beans);
        mPoliceAdapter.notifyDataSetChanged();
    }

    @Override
    public void requestSupport(String hint) {
        showShortToast(hint);
        finish();
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        changeBoxSelect(i);
    }

    class PoliceAdapter extends ArrayAdapter {

        public PoliceAdapter(@NonNull Context context, int resource) {
            super(context, resource);
        }

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            PoliceInfoBean policeInfoBean = datas.get(position);
            View view = getLayoutInflater().inflate(R.layout.item_support_police, null);
            TextView policeName = view.findViewById(R.id.police_name);
            CheckBox checked = view.findViewById(R.id.police_check);

            policeName.setText(policeInfoBean.getName());
            checked.setChecked(map.get(position));

            return view;
        }
    }
    /**
     *
     * @param position
     * @return  返回item改变前的选中状态
     */
    public boolean changeBoxSelect(int position){
        Boolean checked = map.get(position);
        map.put(position,!checked);
        if (checked){
            mIds.remove(datas.get(position).getId());
            showShortToast("取消选中："+datas.get(position).getName());
        }else {
            mIds.add(datas.get(position).getId());
            showShortToast("选中："+datas.get(position).getName());
        }
        mPoliceAdapter.notifyDataSetChanged();
        return checked;
    }
}
